<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Session;
use Cache;

class ApiController extends Controller
{
	
	// http get 请求
	public static function geturl( $url )
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// 将返回结果进行转换
        $res = curl_exec($ch);						// 发送请求
        return $res;								// 将结果返返回
    }
	
	
	// 下载教练图片 并更改数据库
	public function coachInfo()
	{
        // 开始
        // 获取路由
        $route_array = request()->route()->getAction();
        $controller = explode("\\",$route_array['controller']);
        $result = explode("@",$controller[3]);
        // 控制器 	$result[0];
        // 方法 	$result[1];
        $auth_final = '';
        foreach( Session::get('auths') as $key=> $auth_info )
        {
            // 符合条件
            if( $auth_info['controller']==$result[0] && $auth_info['func']==$result[1] )
            {
                $auth_final = true;
                break;
            }else
            {
                $auth_final = false;
            }
        }
        // 权限验证失败
        if( !$auth_final )
        {
            return redirect('/')->with( ['authinfo'=>'没有此项权限'] );
        }
        // 结尾
		set_time_limit(0);
		// 获取 所有教练
		$coaches = DB::table('coach_info')
			-> select('official_id','image_86x120_url','image_130x180_url')
			-> get();
		foreach( $coaches as $key => $coach_info )
		{
			// 定义 文件夹
			$common_dir = "../../image/images/api/coach/".$coach_info->official_id."/";
			// 检测 目录 是否存在
			if( !file_exists($common_dir) )
			{	
				// 创建 目录
				mkdir( $common_dir,0777,true );
			}else
			{
				echo '已下载--教练official_id--'.$coach_info->official_id.'<br/>';
				continue;
			}

			// 定义更新数组
			$data = array();
			// 下载第一张图片
			$first_content = file_get_contents($coach_info->image_86x120_url);

			// 图片后缀
			$first_ext = pathinfo($coach_info->image_86x120_url,PATHINFO_EXTENSION);
			$first = $common_dir."image_86x120_url.".$first_ext;
			file_put_contents( $first, $first_content);
			$data['image_86x120_url'] = "/images/api/coach/".$coach_info->official_id."/image_86x120_url.".$first_ext;
			
			// 下载第二张图片
			$second_content = file_get_contents($coach_info->image_130x180_url);
			// 图片后缀
			$second_ext = pathinfo($coach_info->image_130x180_url,PATHINFO_EXTENSION);
			$second = $common_dir."image_130x180_url.".$second_ext;
			file_put_contents( $second, $second_content);
			$data['image_130x180_url'] = "/images/api/coach/".$coach_info->official_id."/image_130x180_url.".$second_ext;

			$change = DB::table('coach_info')
				-> where('official_id','=',$coach_info->official_id )
				-> update( $data );

			if( $change )
			{
				echo $coach_info->official_id."修改成功<br/>";
			}else
			{
				echo $coach_info->official_id."修改失败<br/>";
			}
			
			
		}
	}
	
	
	// 下载球员图片 并更改数据库
	public function playerInfo()
	{
        // 开始
        // 获取路由
        $route_array = request()->route()->getAction();
        $controller = explode("\\",$route_array['controller']);
        $result = explode("@",$controller[3]);
        // 控制器 	$result[0];
        // 方法 	$result[1];
        $auth_final = '';
        foreach( Session::get('auths') as $key=> $auth_info )
        {
            // 符合条件
            if( $auth_info['controller']==$result[0] && $auth_info['func']==$result[1] )
            {
                $auth_final = true;
                break;
            }else
            {
                $auth_final = false;
            }
        }
        // 权限验证失败
        if( !$auth_final )
        {
            return redirect('/')->with( ['authinfo'=>'没有此项权限'] );
        }
        // 结尾
		set_time_limit(0);
		// 获取 所有球员
		$players = DB::table('player_info')
			-> select('player_id','image_86x120_url','image_130x180_url')
			-> get();
		foreach( $players as $key => $player_info )
		{
			// 定义 文件夹
			$common_dir = "../../image/images/api/player/".$player_info->player_id."/";
			// 检测 目录 是否存在
			if( !file_exists($common_dir) )
			{	
				// 创建 目录
				mkdir( $common_dir,0777,true );
			}else
			{
				echo '已下载--球员['.$player_info->player_id.']<br/>';
				continue;
			}
			
			// 定义更新数组
			$data = array();
			// 下载第一张图片
			$first_content = file_get_contents($player_info->image_86x120_url);
			// 图片后缀
			$first_ext = pathinfo($player_info->image_86x120_url,PATHINFO_EXTENSION);
			$first = $common_dir."image_86x120_url.".$first_ext;
			file_put_contents( $first, $first_content);
			$data['image_86x120_url'] = "/images/api/player/".$player_info->player_id."/image_86x120_url.".$first_ext;
			
			// 下载第二张图片
			$second_content = file_get_contents($player_info->image_130x180_url);
			// 图片后缀
			$second_ext = pathinfo($player_info->image_130x180_url,PATHINFO_EXTENSION);
			$second = $common_dir."image_130x180_url.".$second_ext;
			file_put_contents( $second, $second_content);
			$data['image_130x180_url'] = "/images/api/player/".$player_info->player_id."/image_130x180_url.".$second_ext;
			
			
			$change = DB::table('player_info')
				-> where('player_id','=',$player_info->player_id )
				-> update( $data );
				
			if( $change )
			{
				echo $player_info->player_id."修改成功<br/>";
			}else
			{
				echo $player_info->player_id."修改失败<br/>";
			}
			
			
		}
	}
	
	// 下载球队图片 并更改数据库
	public function teamInfo()
	{
        // 开始
        // 获取路由
        $route_array = request()->route()->getAction();
        $controller = explode("\\",$route_array['controller']);
        $result = explode("@",$controller[3]);
        // 控制器 	$result[0];
        // 方法 	$result[1];
        $auth_final = '';
        foreach( Session::get('auths') as $key=> $auth_info )
        {
            // 符合条件
            if( $auth_info['controller']==$result[0] && $auth_info['func']==$result[1] )
            {
                $auth_final = true;
                break;
            }else
            {
                $auth_final = false;
            }
        }
        // 权限验证失败
        if( !$auth_final )
        {
            return redirect('/')->with( ['authinfo'=>'没有此项权限'] );
        }
        // 结尾
		set_time_limit(0);
		// 获取 所有球队
		$all_team = DB::table('team_info')
			-> select( 'team_id','image_24x24_url','image_87x87_url','image_110x88_url','jpg_24x24_url','jpg_87x87_url','jpg_110x88_url','name' )
			-> get();
		foreach( $all_team as $key => $team_info )
		{
		
			// 定义 文件夹
			$common_dir = "../../image/images/api/team/".$team_info->team_id."/";
			// 检测 目录 是否存在
			if( !file_exists($common_dir) )
			{	
				// 创建 目录
				mkdir( $common_dir,0777,true );
			}else
			{
				echo '已下载--'.$team_info->name."[".$team_info->team_id."]".'<br/>';
				continue;
			}
			

			// 定义更新数组
			$data = array();
			// 下载第一张图片
			$first_content = file_get_contents($team_info->image_24x24_url);
			// 图片后缀
			$first_ext = pathinfo($team_info->image_24x24_url,PATHINFO_EXTENSION);
			$first = $common_dir."image_24x24_url.".$first_ext;
			file_put_contents( $first, $first_content);
			$data['image_24x24_url'] = "/images/api/team/".$team_info->team_id."/image_24x24_url.".$first_ext;

			
			// 下载第二张图片
			$second_content = file_get_contents($team_info->image_87x87_url);
			// 图片后缀
			$second_ext = pathinfo($team_info->image_87x87_url,PATHINFO_EXTENSION);
			$second = $common_dir."image_87x87_url.".$second_ext;
			file_put_contents( $second, $second_content);
			$data['image_87x87_url'] = "/images/api/team/".$team_info->team_id."/image_87x87_url.".$second_ext;
			
			
			// 下载第三张图片
			$third_content = file_get_contents($team_info->image_110x88_url);
			// 图片后缀
			$third_ext = pathinfo($team_info->image_110x88_url,PATHINFO_EXTENSION);
			$third = $common_dir."image_110x88_url.".$third_ext;
			file_put_contents( $third, $third_content);
			$data['image_110x88_url'] = "/images/api/team/".$team_info->team_id."/image_110x88_url.".$third_ext;
			
			
			// 下载第四张图片
			$four_content = file_get_contents( $team_info->jpg_24x24_url );
			// 图片后缀
			$four_ext = pathinfo($team_info->jpg_24x24_url,PATHINFO_EXTENSION);
			$four = $common_dir."jpg_24x24_url.".$four_ext;
			file_put_contents( $four, $four_content);
			$data['jpg_24x24_url'] = "/images/api/team/".$team_info->team_id."/jpg_24x24_url.".$four_ext;
			
			
			
			// 下载第五张图片
			$five_content = file_get_contents( $team_info->jpg_87x87_url );
			// 图片后缀
			$five_ext = pathinfo($team_info->jpg_87x87_url,PATHINFO_EXTENSION);
			$five = $common_dir."jpg_87x87_url.".$four_ext;
			file_put_contents( $five, $five_content);
			$data['jpg_87x87_url'] = "/images/api/team/".$team_info->team_id."/jpg_87x87_url.".$five_ext;
			
			
			// 下载第六张图片
			$six_content = file_get_contents( $team_info->jpg_110x88_url );
			// 图片后缀
			$six_ext = pathinfo($team_info->jpg_110x88_url,PATHINFO_EXTENSION);
			$six = $common_dir."jpg_110x88_url.".$six_ext;
			file_put_contents( $six, $six_content);
			$data['jpg_110x88_url'] = "/images/api/team/".$team_info->team_id."/jpg_110x88_url.".$six_ext;
			
			$change = DB::table('team_info')
				-> where('team_id','=',$team_info->team_id )
				-> update( $data );
			if( $change )
			{
				echo $team_info->team_id."修改成功<br/>";
			}else
			{
				echo $team_info->team_id."修改失败<br/>";
			}
			
		}
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	//  足球 联赛
	public function test($type=1)
	{
		// 接口 密钥
		$appKey = "d93d1ab9172506bb84d01b4d49327fbb";
		// 接口
		switch($type)
		{
			case '1':
				
				// 足球联赛 赛事查询
				// 6个值
				
				// 提供英超，西甲，德甲，意甲，法甲，中超 等赛程
				

				// 英超  saicheng 10条/13个字段 jifenbang 10条/9个字段 10条数据/7个字段
				// 西甲  saicheng 10条/13个字段 jifenbang 10条/9个字段 10条数据/7个字段
				// 德甲  saicheng 9条/12个字段  jifenbang 10条/9个字段 10条数据/7个字段
				// 意甲  saicheng 10条/12个字段 jifenbang 10条/9个字段 10条数据/7个字段
				// 法甲  saicheng 10条/13个字段 jifenbang 10条/9个字段 10条数据/7个字段
				// 中超  saicheng 8条/13个字段  jifenbang 10条/9个字段 10条数据/7个字段
				
				// 联赛名称 
				$league = '中超';
				$api_url = 'http://op.juhe.cn/onebox/football/league?dtype=&league='.$league.'&key='.$appKey;
				
				
				$json = $this::geturl( $api_url );
				$arr = json_decode( $json,true );
				
				// 缓存 已有
				// if( Cache::get('football_league_'.$league) )
				if( '' )
				{
					$arr = Cache::get('football_league_'.$league);
					return '旧的缓存';
				}else
				{
					// 重新 请求接口
					$json = $this::geturl( $api_url );
					$arr = json_decode( $json,true );
					// 查询 成功
					if( $arr['error_code']==0 && $arr['reason']=='查询成功' )
					{
						// 设置 缓存 15分钟
						Cache::put( 'football_league_'.$league,$arr,15 );
						// 关键词
						$key	 = $arr['result']['key'];
						// 联赛id (league_id）
						switch( $key )
						{
							case '英超':
								$league_id = 2;
							break;
							case '西甲':
								$league_id = 3;
							break;
							case '德甲':
								$league_id = 5;
							break;
							case '意甲':
								$league_id = 4;
							break;
							case '法甲':
								$league_id = 6;
							break;
							case '中超':
								$league_id = 128;
							break;
						}
						// 选项卡(暂时没用)
						$tabs 	 = $arr['result']['tabs'];
						// 关键内容
						$views	 = $arr['result']['views'];
						// var_dump( count($views['saicheng1']) );
						if( count($views['saicheng1'])>0 )
						{
							foreach( $views['saicheng1'] as $key => $saicheng1 )
							{
								// 获得日期
								$date = date("Y").'-'.substr( $saicheng1['c2'],0,5 );
								$history = DB::table('saicheng')
										// c2+ c4T1 联合查询
										-> where( 'c2','=', $date )
										-> where( 'c4T1','=', $saicheng1['c4T1'] )
										-> first();
								if( $history )
								{
									echo 'saicheng表--已经插入过了,跳出此次更新<br/>';
								}else
								{
									$saicheng1['c2'] = $date;
									$saicheng1['name'] = $tabs['saicheng1'];
									$saicheng1['league_id'] = $league_id;
									$insert_id = DB::table('saicheng')->insertGetId($saicheng1);
									echo $insert_id.'插入saicheng表成功<br/>';
								}
								
							}
						
						}
						
						
						// 10条数据 每条13个字段
						// var_dump( count($views['saicheng2']) );
						if( count($views['saicheng2'])>0 )
						{
							foreach( $views['saicheng2'] as $key => $saicheng2 )
							{
								
								// 获得日期
								$date = date("Y").'-'.substr( $saicheng2['c2'],0,5 );
								$history = DB::table('saicheng')
										// c2+ c4T1 联合查询
										-> where( 'c2','=', $date )
										-> where( 'c4T1','=', $saicheng2['c4T1'] )
										-> first();
								if( $history )
								{
									echo 'saicheng表--已经插入过了,跳出此次更新<br/>';
								}else
								{
									$saicheng2['c2'] = $date;
									$saicheng2['name'] = $tabs['saicheng2'];
									$saicheng2['league_id'] = $league_id;
									$insert_id = DB::table('saicheng')->insertGetId($saicheng2);
									echo $insert_id.'插入saicheng表成功<br/>';
								}
								
							}
						}
						
						
						// var_dump( count($views['saicheng3']) );
						if( count($views['saicheng3'])>0 )
						{
							foreach( $views['saicheng3'] as $key => $saicheng3 )
							{
								// 获得日期
								$date = date("Y").'-'.substr( $saicheng3['c2'],0,5 );
								$history = DB::table('saicheng')
										// c2+ c4T1 联合查询
										-> where( 'c2','=', $date )
										-> where( 'c4T1','=', $saicheng3['c4T1'] )
										-> first();
								if( $history )
								{
									echo 'saicheng表--已经插入过了,跳出此次更新<br/>';
								}else
								{
									$saicheng3['c2'] = $date;
									$saicheng3['name'] = $tabs['saicheng3'];
									$saicheng3['league_id'] = $league_id;
									$insert_id = DB::table('saicheng')->insertGetId($saicheng3);
									echo $insert_id.'插入saicheng表成功<br/>';
								}
								
							}
							
						}
						
						// 10条数据 每条9个字段
						// var_dump( count($views['jifenbang']) );
						if( count($views['jifenbang'])>0 )
						{
							
							foreach( $views['jifenbang'] as $key => $jifenbang )
							{
								$history = DB::table('jifenbang')
										-> where('c1','=', intval($jifenbang['c1']) )
										-> where('league_id','=', $league_id )
										-> first();
								if( $history )
								{
									echo 'jifenbang--已经插入过了,跳出此次更新<br/>';
								}else
								{
									$jifenbang['name'] = $tabs['saicheng1'];
									$jifenbang['league_id'] = $league_id;
									$jifenbang['create_at'] = date('Y-m-d H:i:s');
									$insert_id = DB::table('jifenbang')->insertGetId($jifenbang);
									echo $insert_id.'插入jifenbang成功<br/>';
								}
								
							}
							
						}
						
						
						// 10条数据 每条7个字段
						// var_dump( count($views['sheshoubang']) );
						if( count($views['sheshoubang'])>0 )
						{
							foreach( $views['sheshoubang'] as $key => $sheshoubang )
							{
								$history = DB::table('sheshoubang')
										-> where('c1','=', intval($sheshoubang['c1']) )
										-> where('league_id','=', $league_id )
										-> first();
								if( $history )
								{
									echo 'sheshoubang已经插入过了,跳出此次更新<br/>';
								}else
								{
									$sheshoubang['create_at'] = date('Y-m-d H:i:s');
									$sheshoubang['league_id'] = $league_id;
									$insert_id = DB::table('sheshoubang')->insertGetId($sheshoubang);
									echo $insert_id.'插入sheshoubang成功<br/>';
								}
								
							}
						}
						dd('结束');
					}else
					{
						dd('请求失败');
					}
				}
				
				
			break;
			case '2':
				// 球队 赛事查询
				// 球队名称
				$team = '皇家马德里';
				
				$api_url = 'http://op.juhe.cn/onebox/football/team?key='.$appKey.'&team='.$team;
				
				// 缓存 已有
				// if( Cache::get('football_team_'.$team) )
				if( '' )
				{
					$arr = Cache::get('football_team_'.$team);
					
				}else
				{
					// 请求 接口
					$json = $this::geturl( $api_url );
					$arr = json_decode( $json,true );
					// 查询 成功
					if( $arr['error_code']==0 && $arr['reason']=='查询成功' )
					{
						// 设置 缓存 一天
						Cache::put( 'football_team_'.$team,$arr,60*24 );
						// 关键词
						$key	 = $arr['result']['key'];
						// 关键内容
						$list	 = $arr['result']['list'];
						// 6条数据 每条15个字段
						// var_dump( count($list) );
						if( count($list)>0 )
						{
							foreach( $list as $key => $team_info )
							{
								$history = DB::table('team')
										-> where('c1','=',$team_info['c1'] )
										-> where('c2','=', $team_info['c2'] )
										-> first();
								if( $history )
								{
									// echo '已经插入过了,跳出此次更新<br/>';
								}else
								{
									$insert_id = DB::table('team')->insertGetId($team_info);
									echo $insert_id.'插入成功<br/>';
								}
								
							}
						}
						
						dd('结束');
						
					}else
					{
						dd('请求失败');
					}
					
				}
				
			break;
			case '3':
				// 球队对战赛 赛程查询  fight
				$hteam='皇家马德里';
				$vteam='阿尔梅里亚';
				$api_url = 'http://op.juhe.cn/onebox/football/combat?key='.$appKey.'&hteam='.$hteam.'&vteam='.$vteam;
				
				// 缓存 已有
				// if( Cache::get('football_combat_'.$hteam.'_'.$vteam) )
				if( '' )
				{
					$arr = Cache::get('football_combat_'.$hteam.'_'.$vteam);
				}else
				{
					$json = $this::geturl( $api_url );
					$arr = json_decode( $json,true );
					// 查询 成功
					if( $arr['error_code']==0 && $arr['reason']=='查询成功' )
					{
						// 设置 缓存 一天
						Cache::put( 'football_combat_'.$hteam.'_'.$vteam,$arr,60*24 );
						// 关键词
						$key	 = $arr['result']['key'];
						// 关键内容
						$list	 = $arr['result']['list'];
						
						// 2条数据 每条16个字段
						// var_dump( count($list) );
						if( count($list)>0 )
						{
							foreach( $list as $key => $fight_info )
							{
								$history = DB::table('combat')
										-> where('date','=',$fight_info['date'] )
										-> where('team1','=', $fight_info['team1'] )
										-> first();
								if( $history )
								{
									echo '已经插入过了,跳出此次更新<br/>';
								}else
								{
									$insert_id = DB::table('combat')->insertGetId($fight_info);
									echo $insert_id.'插入成功<br/>';
								}
								
							}
							
						}
						dd('结束');
					}else
					{
						dd('请求失败');
					}
					
				}
			break;
		}
		
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	//  足球赛事球员 信息大全
	public function test2($type=10)
	{
		// 接口 密钥
		$appKey = "66631589e99f9f2267f49d545797cfcd";
		// 接口
		switch( $type )
		{
			case '1':
				// 足球球队教练信息
				// 联赛id (可通过"足球积分榜"接口获取)
				$league_id = 128;
				// 球队id (可通过"足球积分榜"接口获取)
				$team_id = 3148;
				$api_url = "http://v.juhe.cn/football/coach_info?league_id=".$league_id.'&team_id='.$team_id.'&key='.$appKey;
				
				// 缓存 已有
				// if( Cache::get('football_coach_info_'.$league_id.'_'.$team_id) )
				if( '' )
				{
					$arr = Cache::get('football_coach_info_'.$league_id.'_'.$team_id);
				}else
				{
					$json = $this::geturl( $api_url );
					$arr = json_decode( $json,true );
					// 查询 成功
					if( $arr['error_code']==0 && $arr['reason']=='success' )
					{
						// 设置 缓存 一周
						Cache::put('football_coach_info_'.$league_id.'_'.$team_id,$arr,60*24*7 );
						// 1条数据 每条22个字段
						// var_dump( count($arr['result']) );
						// dd( $arr['result'] );
						if( count($arr['result'])>0 )
						{
							foreach( $arr['result'] as $key => $coach_info )
							{
								$history = DB::table('coach_info')
										-> where('official_id','=',$coach_info['official_id'] )
										-> first();
								if( $history )
								{
									// echo '已经插入过了,跳出此次更新<br/>';
								}else
								{
									$insert_id = DB::table('coach_info')->insertGetId($coach_info);
									echo $insert_id.'插入成功<br/>';
								}
								
							}
							
						}
						dd('结束');
					}else
					{
						dd('请求失败');
					}
					
				}
				
			break;
			case '2':
				// 足球比赛阵容
				// 比赛id 通过第10个接口获取
				$game_id = 5000919;
				$api_url = "http://v.juhe.cn/football/lineup.php?game_id=".$game_id."&key=".$appKey;
				$json = $this::geturl( $api_url );
				$arr = json_decode( $json,true );
				
				
				// 查询 成功
				if( $arr['error_code']==0 && $arr['reason']=='success' )
				{
					// 1条数据 每条13个字段 包含2个子节
					// var_dump( count($arr['result']) );
					// dd($arr['result']);
					if( count($arr['result'])>0 )
					{
						foreach( $arr['result'] as $key => $lineup_info )
						{
						
							// 18条数据 每条9个字段
							// 主队
							$lineup_info['home_lineup'] = json_encode($lineup_info['home_lineup']);
							// 客队
							$lineup_info['away_lineup'] = json_encode($lineup_info['away_lineup']);
							
							$history = DB::table('lineup')
									-> where('game_id','=',$lineup_info['game_id'] )
									-> first();
							if( $history )
							{
								echo 'lineup--'.$key.'已经插入过了<br/>';
							}else
							{
								// 主表
								$insert_id = DB::table('lineup')->insertGetId($lineup_info);
								if( $insert_id )
								{
									echo 'lineup--'.$key.'插入成功<br/>';
								}else
								{
									echo 'lineup--'.$key.'插入失败<br/>';
								}
								
							}
							
							
						}
					}
					dd('结束');
				}else
				{
					dd('请求失败');
				}
					
				
				
			break;
			case '3':
				// 足球比赛直播事件
				// 比赛id
				$game_id = 5000919;
				$api_url = "http://v.juhe.cn/football/live_event.php?game_id=".$game_id."&key=".$appKey;
				
				// 缓存 已有
				if( Cache::get('live_event') )
				{
					$arr = Cache::get('live_event');
					// echo '走了缓存,不插入';
					// return '123';
				}else
				{
					$json = $this::geturl( $api_url );
					$arr = json_decode( $json,true );
					// 设置 缓存 15分钟
					Cache::put( 'live_event',$arr,15 );
				}
				// 查询 成功
				if( $arr['error_code']==0 && $arr['reason']=='success' )
				{
					// 19条数据 每条23个字段
					// var_dump( count($arr['result']) );
					if( count($arr['result'])>0 )
					{
						foreach( $arr['result'] as $key => $live_info )
						{
							// 主表
							// $insert_id = DB::table('live_event')->insertGetId($live_info);
						}
					}
					// dd('插入成功1');
				}else
				{
					dd('请求失败');
				}
			break;
			case '4':
				// 足球球队统计
				// 比赛id
				$game_id = 7882149;
				$team_id = 3148;
				$season_id = 2015;
				$api_url = "http://v.juhe.cn/football/teamstats.php?game_id=".$game_id."&team_id=".$team_id."&season_id=".$season_id."&key=".$appKey;
				
				// 缓存 已有
				if( Cache::get('teamstats') )
				{
					$arr = Cache::get('teamstats');
					// echo '走了缓存,不插入';
					// return '123';
				}else
				{
					$json = $this::geturl( $api_url );
					$arr = json_decode( $json,true );
					// 设置 缓存 15分钟
					Cache::put( 'teamstats',$arr,15 );
				}
				
				// 查询 成功
				if( $arr['error_code']==0 && $arr['reason']=='success' )
				{
					// 1条数据 每条73个字段
					// var_dump( count($arr['result']) );
					if( count($arr['result'])>0 )
					{
						foreach( $arr['result'] as $key => $teamstats_info )
						{
							// 主表
							// $insert_id = DB::table('teamstats')->insertGetId($teamstats_info);
						}
					}
					// dd('插入成功1');
				}else
				{
					dd('请求失败');
				}
			break;
			case '5':
				// 球员单场统计
				// 比赛id
				$game_id = 7882141;
				$team_id = 3152;
				$season_id = 2015;
				$api_url = "http://v.juhe.cn/football/player_single_stats.php?game_id=".$game_id."&season_id=".$season_id."&team_id=".$team_id."&key=".$appKey;
				
				// 缓存 已有
				if( Cache::get('player_single_stats') )
				{
					$arr = Cache::get('player_single_stats');
					// echo '走了缓存,不插入';
					// return '123';
				}else
				{
					$json = $this::geturl( $api_url );
					$arr = json_decode( $json,true );
					// 设置 缓存 15分钟
					Cache::put( 'player_single_stats',$arr,15 );
				}
				
				// 查询 成功
				if( $arr['error_code']==0 && $arr['reason']=='success' )
				{
					// 18条数据 每条88个字段
					// var_dump( count($arr['result']) );
					if( count($arr['result'])>0 )
					{
						foreach( $arr['result'] as $key => $player_single_info )
						{
							// 主表
							// $insert_id = DB::table('player_single_stats')->insertGetId($player_single_info);
						}
					}
					// dd('插入成功1');
				}else
				{
					dd('请求失败');
				}
			break;
			case '6':
				// 球员赛季统计
				// 比赛id
				$player_id = 552;
				$team_id = 116;
				$season_id = 2015;
				
				$api_url = "http://v.juhe.cn/football/player_season_stats.php?player_id=".$player_id."&season_id=".$season_id."&team_id=".$team_id."&key=".$appKey;
				
				// 缓存 已有
				if( Cache::get('player_season_stats') )
				{
					$arr = Cache::get('player_season_stats');
					// echo '走了缓存,不插入';
					// return '123';
				}else
				{
					$json = $this::geturl( $api_url );
					$arr = json_decode( $json,true );
					// 设置 缓存 15分钟
					Cache::put( 'player_season_stats',$arr,15 );
				}
				
				// 查询 成功
				if( $arr['error_code']==0 && $arr['reason']=='success' )
				{
					// 1条数据 36个字段
					// var_dump( count($arr['result']) );
					// dd($arr['result']);
					if( count($arr['result'])>0 )
					{
						foreach( $arr['result'] as $key => $player_season_info )
						{
							// 主表
							// $insert_id = DB::table('player_season_stats')->insertGetId($player_season_info);
						}
					}
					// dd('插入成功1');
				}else
				{
					dd('请求失败');
				}
			break;
			case '7':
				// 足球积分榜
				
				// 联赛id 
				$league_id = 128;
				
				// 英超=2
				// 西甲=3
				// 意甲=4
				// 德甲=5
				// 法甲=6
				// 欧冠=9
				// 中超=128 有数据 目前其他都没有
				// 英足总杯=110
				// 英联杯=111
				// 国王杯(西)=112
				// 德国杯=113
				// 意大利杯=114
				// 法国杯=115
				
				$api_url = "http://v.juhe.cn/football/scorerank.php?league_id=".$league_id."&key=".$appKey;
				
				$json = $this::geturl( $api_url );
				$arr = json_decode( $json,true );
				// echo $league_id;
				// dd($arr);
				
				// 查询 成功
				if( $arr['error_code']==0 && $arr['reason']=='success' )
				{
					// 16条数据 22个字段
					// var_dump( count($arr['result']) );
					// dd($arr['result'][0]);
					// 有数据
					if( count($arr['result'])>0 )
					{
						foreach( $arr['result'] as $key => $scorerank_info )
						{
							$history = DB::table('scorerank')
								-> where( 'id','=',$scorerank_info['id'] )
								-> first();
							if( !empty($history) )
							{
								echo 'scorerank--'.$key.'已经插入过了<br/>';
							}else
							{
								// 主表 特殊 返回数据自带id
								$insert_id = DB::table('scorerank')
													->insertGetId($scorerank_info);
								echo 'scorerank--'.$key.'插入成功<br/>';
							}
							
						}
					}
					dd('结束');
				}else
				{
					dd('请求失败');
				}
				
			break;
			case '8':
				// 足球射手榜
				// 联赛id 
				$league_id = 110;
				$season_id = 2015;
				$api_url = "http://v.juhe.cn/football/goalrank.php?league_id=".$league_id."&season_id=".$season_id ."&key=".$appKey;
				
				// 缓存 已有
				if( Cache::get('goalrank') )
				{
					$arr = Cache::get('goalrank');
					// echo '走了缓存,不插入';
					// return '123';
				}else
				{
					$json = $this::geturl( $api_url );
					$arr = json_decode( $json,true );
					// 设置 缓存 15分钟
					Cache::put( 'goalrank',$arr,15 );
				}
				
				// 查询 成功
				if( $arr['error_code']==0 && $arr['reason']=='success' )
				{
					// 100条数据 14个字段
					// var_dump( count($arr['result']) );
					dd($arr['result'][0]);
					if( count($arr['result'])>0 )
					{
						foreach( $arr['result'] as $key => $goalrank_info )
						{
							// 主表
							// $insert_id = DB::table('goalrank')->insertGetId($goalrank_info);
						}
					}
					// dd('插入成功1');
				}else
				{
					dd('请求失败');
				}
			break;
			case '9':
				// 足球助攻榜
				// 联赛id 
				$league_id = 110;
				// 赛季ID
				$season_id = 2015;
				$api_url = "http://v.juhe.cn/football/assistrank.php?league_id=".$league_id."&season_id=".$season_id."&key=".$appKey;
				
				// 缓存 已有
				if( Cache::get('assistrank') )
				{
					$arr = Cache::get('assistrank');
					// echo '走了缓存,不插入';
					// return '123';
				}else
				{
					$json = $this::geturl( $api_url );
					$arr = json_decode( $json,true );
					// 设置 缓存 15分钟
					Cache::put( 'assistrank',$arr,15 );
				}
				
				// 查询 成功
				if( $arr['error_code']==0 && $arr['reason']=='success' )
				{
					// 100条数据 14个字段
					// var_dump( count($arr['result']) );
					dd( $arr['result'] );
					// dd($arr['result'][0]);
					if( count($arr['result'])>0 )
					{
						foreach( $arr['result'] as $key => $assistrank_info )
						{
							// 主表
							// $insert_id = DB::table('assistrank')->insertGetId($assistrank_info);
						}
					}
					// dd('插入成功1');
				}else
				{
					dd('请求失败');
				}
			break;
			case '10':
				
				// 足球赛程查询
				// 联赛id 
				$league_id = 2;
				// 赛季ID
				$season_id = 2014;
				//
				$game_id = 5000760;
				// 有问题 没通
				$api_url = "http://v.juhe.cn/football/query_schedule.php?league_id=".$league_id."&season_id=".$season_id."&game_id=".$game_id."&key=".$appKey;
				$json = $this::geturl( $api_url );
				$arr = json_decode( $json,true );
					
				
				
				// 查询 成功
				if( $arr['error_code']==0 && $arr['reason']=='success' )
				{
					// 100条数据 14个字段
					// var_dump( count($arr['result']) );
					dd( $arr['result'] );
					// dd($arr['result'][0]);
					if( count($arr['result'])>0 )
					{
						foreach( $arr['result'] as $key => $query_schedule_info )
						{
							// 主表
							// $insert_id = DB::table('query_schedule')->insertGetId($query_schedule_info);
						}
					}
					// dd('插入成功1');
				}else
				{
					dd('请求失败');
				}
			break;
			
			case '11':
				// 球队信息
				// 联赛id 
				$league_id = 128;
				//
				$team_id = 3148;
				$api_url = "http://v.juhe.cn/football/team_info.php?league_id=".$league_id."&team_id=".$team_id."&key=".$appKey;
				
				$json = $this::geturl( $api_url );
				$arr = json_decode( $json,true );
					
				// 查询 成功
				if( $arr['error_code']==0 && $arr['reason']=='success' )
				{
					// 2条数据 25个字段
					// var_dump( count($arr['result']) );
					// dd( $arr['result'] );
					// dd($arr['result'][0]);
					if( count($arr['result'])>0 )
					{
						foreach( $arr['result'] as $key => $team_info )
						{
							// 主表
							// $insert_id = DB::table('team_info')->insertGetId($team_info);
						}
					}
					// dd('插入成功1');
				}else
				{
					dd('请求失败');
				}
			break;
			
			
			case '12':
				// 足球球员信息
				// 联赛id 
				$league_id = 128;
				$team_id = 3148;
				$player_id = 103170;
				$api_url = "http://v.juhe.cn/football/player_info.php?league_id=".$league_id."&team_id=".$team_id."&player_id=".$player_id."&key=".$appKey;
				
				// 缓存 已有
				if( Cache::get('player_info') )
				{
					$arr = Cache::get('player_info');
					// echo '走了缓存,不插入';
					// return '123';
				}else
				{
					$json = $this::geturl( $api_url );
					$arr = json_decode( $json,true );
					// 设置 缓存 15分钟
					Cache::put( 'player_info',$arr,15 );
				}
				
				// 查询 成功
				if( $arr['error_code']==0 && $arr['reason']=='success' )
				{
					// 1条数据 32个字段
					// var_dump( count($arr['result']) );
					// dd( $arr['result'] );
					dd($arr['result'][0]);
					if( count($arr['result'])>0 )
					{
						foreach( $arr['result'] as $key => $player_info )
						{
							// 主表
							// $insert_id = DB::table('player_info')->insertGetId($player_info);
						}
					}
					// dd('插入成功1');
				}else
				{
					dd('请求失败');
				}
			break;
		}
	}
	
	
	
}
